package com.ax.center.ExceptionHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.Model;

/**
 * @author zy
 * @date 2023/7/4 21:31
 */
@ControllerAdvice
public class CustomErrorController implements ErrorController {

    private static final Logger log = LoggerFactory.getLogger(CustomErrorController.class);

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request, HttpServletResponse response, Model model) {
        // 获取错误状态码
        int statusCode = response.getStatus();

        // 根据状态码进行逻辑判断和处理
        if (statusCode == HttpStatus.NOT_FOUND.value()) {
            // 处理 404 错误
            model.addAttribute("error", "Page Not Found");
            model.addAttribute("message", "The requested page was not found");
            return "error-404";
        } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
            // 处理 500 错误
            model.addAttribute("error", "Internal Server Error");
            model.addAttribute("message", "An unexpected error occurred");
            return "error-500";
        } else {
            // 其他错误情况
            model.addAttribute("error", "Error");
            model.addAttribute("message", "An error occurred");
            return "error";
        }
    }



}
